'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 激活码
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var rewardService = require('../../../services/rewardService');
var PlayerManager = require('../../../libs/playerManager');
var cdkInfoModel = require('../../../models/cdkInfoModel.js');
var cdkTypeModel = require('../../../models/cdkTypeModel.js');
var constantUtil = require('../../../util/constantUtil');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 验证激活码
 */
Handler.prototype.verify = function (msg, session, next) {
    var role_id = session.uid;
    var cdk_id = msg.cdk_id;
    if (!cdk_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var cdk_model;
    var cdk_info_model;
    var cdk_type_model;
    var reward_list;

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'cdk', 'build', 'soldier', 'bag', 'task', 'daily_task', 'achieve', 'tech', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                cdk_model = player_manager.cdk;
                cdkInfoModel.getCdkInfo(cdk_id, cb);
            },
            function (model, cb) {
                cdk_info_model = model;

                // 激活码是否有效
                if (!cdk_info_model) {
                    return next(null, {
                        code: Code.USER_CDK_ID_NOT_USE
                    });
                }

                cdkTypeModel.getCdkType(cdk_info_model.cdk_type, cb);
            },
            function (model, cb) {
                cdk_type_model = model;

                // 激活码类型无效
                if (!cdk_type_model) {
                    return next(null, {
                        code: Code.USER_CDK_ID_NOT_USE
                    });
                }

                var share_cdk_type_min = constantUtil.value('share_cdk_type_min');
                var share_cdk_type_max = constantUtil.value('share_cdk_type_max');
                var cdk_type = cdk_type_model.cdk_type;
                var is_share_cdk_type = (cdk_type >= share_cdk_type_min && cdk_type <= share_cdk_type_max);

                if (is_share_cdk_type) {
                    if (cdk_info_model.cdk_used >= cdk_type_model.limit_times) {
                        return next(null, {
                            code: Code.USER_CDK_ID_NOT_USE
                        });
                    }
                } else {
                    if (cdk_info_model.cdk_used > 0 && cdk_info_model.cdk_used !== role_id) {
                        return next(null, {
                            code: Code.USER_CDK_ID_NOT_USE
                        });

                    }

                    // 设置使用者
                    cdk_info_model.cdk_used = role_id;
                }

                if (is_share_cdk_type) {
                    // 公众激活码，每人只能用一次
                    if (!cdk_model.verify(cdk_type_model.cdk_type, 1)) {
                        return next(null, {
                            code: Code.USER_HAVE_USED_CDK_TYPE
                        });
                    }
                    cdk_info_model.cdk_used++;
                } else {
                    // 按类型验证礼包使用次数，未超限，则加入到玩家的激活码列表
                    if (!cdk_model.verify(cdk_type_model.cdk_type, cdk_type_model.limit_times)) {
                        return next(null, {
                            code: Code.USER_HAVE_USED_CDK_TYPE
                        });
                    }
                }

                reward_list = cdk_type_model.reward_list;
                rewardService.addRewardListToRole(player_manager, reward_list, moduleType.MDL_OTHER, operationType.OPR_OTHER_CDK_REWARD);
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            cdk_info_model.save();
            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: reward_list
                }
            });
        }
    );
};
